//
// Created on 2022/8/18.
//

#include "pinyin_helper.h"
#include "pinyin_data.h"
#include <hilog/log.h>
#define LOGD(...) ((void)OH_LOG_Print(LOG_APP, LOG_DEBUG, LOG_DOMAIN, "[NativeAPI]", __VA_ARGS__))


using namespace std;

namespace OHOS {
namespace PINYIN {

PinyinHelper::PinyinHelper() {
//   LOGD('1333");
}

//const string text
int PinyinHelper::convert(napi_env env,std::string sourceText, std::string &resultText) {


    if(sourceText.empty()){

        return -1;
    }
    resultText.clear();

    // 分割字符 #把中文分割，string.at()获取的是字符
    vector<string> characters;
    size_t num = sourceText.size();
    size_t i = 0;
    char temp;
    size_t size;
    while (i < num) {
        size = 1;
        if (sourceText[i] & 0x80) {
            temp = sourceText[i] << 1;
            do {
                temp <<= 1;
                ++size;
            } while (temp & 0x80);
        }
        characters.push_back(sourceText.substr(i, size));
        i += size;
    }

    int sourceNum = (int)characters.size();

    // 汉字拼音的数据
    PinyinData *pinyindata = new PinyinData();
    // 返回的结果
    vector<vector<string>> resultList;

    for (string text : characters) {

        vector<string> result;

        int ret = pinyindata->pinyinIndexOf(text, result);
        if(ret==0){
            // 有拼音返回
            resultList.push_back(result);

        }
        else{
            // 未转换的字符，存储起来
            /**
             TODO：如果能检测到某一串字符不是中文的话，可以放在一起，在后面组合列表时，节约排列的时间
             // 截取汉字
             //    int startIndex = 0;
             //    int endIndex = 0;
             */
            vector<string> tmp;
            tmp.push_back(text);
            resultList.push_back(tmp);
        }

    }

    delete pinyindata;


    // 把二维数组 排列组合成字符串
    vector<int> nowIndexList;
    vector<int> maxIndexList;
    int sum_maxIndexList = 0;
    for (size_t i = 0; i < sourceNum; i++)
    {
        nowIndexList.push_back(0);
        maxIndexList.push_back((int)resultList[i].size() - 1);
        sum_maxIndexList += maxIndexList[i];
    }

    // --- 第一次组合（所有采用第一个）
    bool mustCombination = sum_maxIndexList > 0 ? true : false;
    for (size_t i = 0; i < sourceNum; i++)
    {
        resultText += resultList[i][0]+" ";
    }


    //  循环遍历
    while (mustCombination)
    {
        // 组合排列
        bool alreadyRunOnce = false;
        for (size_t i = 0; i < sourceNum; i++)
        {
            if (alreadyRunOnce)
            {
                break;
            }

            if (maxIndexList[i] != 0)
            {
                if (nowIndexList[i] < maxIndexList[i])
                {
                    alreadyRunOnce = true;
                    nowIndexList[i]++;
                }
                else if (nowIndexList[i] == maxIndexList[i])
                {
                    nowIndexList[i] = 0;
                }
            }
        }

        // 组合输出字符
        resultText += "| ";
        for (size_t i = 0; i < sourceNum; i++)
        {
            resultText += resultList[i][nowIndexList[i]]+" ";
        }

        // 退出条件
        bool canOut = true;
        for (size_t i = 0; i < sourceNum; i++)
        {
            if (nowIndexList[i] != maxIndexList[i])
            {
                canOut = false;
                break;
            }
        }
        if (canOut)
        {
            break;
        }
    }

    
    return 0;
}


} // namespace PINYIN

} // namespace OHOS
